//
//  XMN_PostsViewController.m
//  B段项目
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "XMN_PostsViewController.h"
#import "XMN_PostsDetailCell.h"
#import "XMN_PostsList.h"

@interface XMN_PostsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, strong)NSDictionary *headerDic;
@property(nonatomic, strong)myView *headerView;

@property(nonatomic, strong)myTitleLabel *titleLabel;
@property(nonatomic, strong)UIImageView *covImageView;
@property(nonatomic, strong)UILabel *nameLabel;
@property(nonatomic, strong)UILabel *timeLabel;
@property(nonatomic, strong)UIImageView *headerImageView;
@property(nonatomic, strong)myContectLabel *contentLabel;

@property(nonatomic, assign)NSInteger start;
@property(nonatomic, strong)XMN_ProgressHUD *HUD;
@end

@implementation XMN_PostsViewController

-(void)loadView{
    [super loadView];
    
    _dataSourceArray = [NSMutableArray array];
    _headerDic = [NSDictionary dictionary];
    
    [self getFirstDataByURLString];
   
    //初始化tableView
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, Height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView NightWithType:UIViewColorType2];
    [self.view addSubview:_tableView];
    
    //创建headerView
    _headerView = [[myView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 650 * FitHeight)];
    [self creatHeaderView];
    _tableView.tableHeaderView = _headerView;
    
//    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        if (_dataSourceArray.count != 0) {
//            [_dataSourceArray removeAllObjects];
//        }
//        [self getFirstDataByURLString];
//    }];
//    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        _start++;
//        [self getBeforeDataByURLString];
//    }];
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(getFirstDataByURLString)];
    _tableView.header = header;
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 1; i <= 9; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%lda", i]];
        [array addObject:image];
    }
    [header setImages:array duration:1 forState:MJRefreshStatePulling];
    header.lastUpdatedTimeLabel.hidden = YES;
    [header setTitle:@"加载中 ..." forState:MJRefreshStateRefreshing];
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(getBeforeDataByURLString)];
    _tableView.footer = footer;
    [footer setImages:array duration:1 forState:MJRefreshStateRefreshing];
    [_tableView.header beginRefreshing];


}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _titleString;
    
    _HUD = [[XMN_ProgressHUD alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, Height)];
    _HUD.backgroundColor = [UIColor colorWithRed:0.89 green:0.89 blue:0.9 alpha:1];
    _HUD.XMN_label.text = @"Waiting...";
    [self.view addSubview:_HUD];
    // Do any additional setup after loading the view.
}

-(void)creatHeaderView{
    _titleLabel = [[myTitleLabel alloc]initWithFrame:CGRectMake(10 * FitWidth, 10 * FitHeight, ScreenWidth - 20 * FitWidth, 30 * FitHeight)];
    [_headerView addSubview:_titleLabel];
    
    _covImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y + _titleLabel.frame.size.height + 10 * FitHeight, 40 * FitWidth, 40 * FitWidth)];
    _covImageView.layer.masksToBounds = YES;
    _covImageView.layer.cornerRadius = 20 * FitWidth;
    [_headerView addSubview:_covImageView];
    
    _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(_covImageView.frame.origin.x + _covImageView.frame.size.width + 10 * FitWidth, _covImageView.frame.origin.y + 10 * FitHeight, 150 * FitWidth, 20 * FitHeight)];
    _nameLabel.font = [UIFont systemFontOfSize:11];
    _nameLabel.textColor = [UIColor grayColor];
    [_headerView addSubview:_nameLabel];
    
    _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth - 110 * FitWidth, _nameLabel.frame.origin.y, 100 * FitWidth, _nameLabel.frame.size.height)];
    _timeLabel.font = [UIFont systemFontOfSize:11];
    _timeLabel.textAlignment = 2;
    _timeLabel.textColor = [UIColor grayColor];
    [_headerView addSubview:_timeLabel];
    
    _headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20 * FitWidth, _covImageView.frame.origin.y + _covImageView.frame.size.height + 20 * FitHeight, ScreenWidth - 40 * FitWidth, 400 * FitHeight)];
    [_headerView addSubview:_headerImageView];
    
    _contentLabel = [[myContectLabel alloc]initWithFrame:CGRectMake(10 * FitWidth, _headerImageView.frame.origin.y + _headerImageView.frame.size.height + 20 * FitHeight, _headerImageView.frame.size.width, 100 * FitHeight)];
    _contentLabel.numberOfLines = 0;
    [_headerView addSubview:_contentLabel];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10 * FitWidth, _contentLabel.frame.origin.y + 9 * FitHeight + _contentLabel.frame.size.height, ScreenWidth - 10 * FitWidth, 1 * FitHeight)];
    label.backgroundColor = [UIColor colorWithRed:0.89 green:0.89 blue:0.9 alpha:1];
    [_headerView addSubview:label];
    
}

#pragma mark - tableView相关设置
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSourceArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    XMN_PostsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[XMN_PostsDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_dataSourceArray.count == 0) {
        return cell;
    }
    cell.postsList = [_dataSourceArray objectAtIndex:indexPath.row];
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90 * FitHeight;
}

#pragma mark - 数据解析
-(void)getFirstDataByURLString{
    if ([_tableView.header isRefreshing]) {
        if (_dataSourceArray.count != 0)
        {
            [_dataSourceArray removeAllObjects];
        }
        _start = 0;

    }
    [HTTPMethod getDataByString:@"http://api2.pianke.me/group/posts_info" BODYDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&contentid=%@&deviceid=DD7B485B-B4B6-4DBA-849E-3A2A14F94824&version=3.0.5", _idString]] WithDataBlock:^(id data) {
//        NSLog(@"%@", data);
        _headerDic = [[data objectForKey:@"data"] objectForKey:@"postsinfo"];
        _titleLabel.text = [_headerDic objectForKey:@"title"];
        [_headerImageView sd_setImageWithURL:[NSURL URLWithString:[[_headerDic objectForKey:@"shareinfo"] objectForKey:@"pic"]]];
        [_headerImageView sd_setImageWithURL:[NSURL URLWithString:[[_headerDic objectForKey:@"shareinfo"] objectForKey:@"pic"]] placeholderImage:[UIImage imageNamed:@"XMN_Shequ_Big"]];
        _nameLabel.text = [[_headerDic objectForKey:@"userinfo"] objectForKey:@"uname"];
        _timeLabel.text = [_headerDic objectForKey:@"addtime_f"];
        [_covImageView sd_setImageWithURL:[NSURL URLWithString:[[_headerDic objectForKey:@"userinfo"] objectForKey:@"icon"]]];
        [_covImageView sd_setImageWithURL:[NSURL URLWithString:[[_headerDic objectForKey:@"userinfo"] objectForKey:@"icon"]] placeholderImage:[UIImage imageNamed:@"SHD_Cov"]];
        _contentLabel.text = _contentString;
        
        NSArray *array = [[data objectForKey:@"data"] objectForKey:@"commentlist"];
        for (NSDictionary *dic in array) {
            XMN_PostsList *postsList = [[XMN_PostsList alloc]init];
            [postsList setValuesForKeysWithDictionary:dic];
            [_dataSourceArray addObject:postsList];
        }
        [_tableView.header endRefreshing];
        [_tableView reloadData];
        [_HUD removeFromSuperview];
    }];
}

-(void)getBeforeDataByURLString{
    _start ++;
    [HTTPMethod getDataByString:@"http://api2.pianke.me/group/posts_comment" BODYDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&contentid=%@&deviceid=DD7B485B-B4B6-4DBA-849E-3A2A14F94824&limit=10&sort=desc&start=%ld&version=3.0.5", _idString, _start * 10]] WithDataBlock:^(id data) {
//        NSLog(@"%@", data);
        NSArray *array = [[data objectForKey:@"data"] objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            XMN_PostsList *postsList = [[XMN_PostsList alloc]init];
            [postsList setValuesForKeysWithDictionary:dic];
            [_dataSourceArray addObject:postsList];
        }
        [_tableView.footer endRefreshing];
        [_tableView reloadData];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
